//
//  MZRecommendLastViewController.m
//  OurTourism
//
//  Created by 孟镇 on 15/11/27.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "MZRecommendLastViewController.h"
#import "Header.h"
#import "MZRecommendModel.h"
#import "MZRecommendPhotosViewController.h"
#import "StarView.h"
#import "MZRecommendLastTableViewCell.h"
#import "MZDescriptionViewController.h"
#import "MZAddressViewController.h"
#import "MZWebsiteViewController.h"
#import "MZFooterTableViewCell.h"
#import "YBTraveldetialViewController.h"
#import "CoreDataManager.h"
#import "MuDiDi.h"
#import "landingViewController.h"

@interface MZRecommendLastViewController () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UIView *naviView;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) MBProgressHUD *HUB;
@property(nonatomic, strong) UILabel *naviLabel;
@property(nonatomic, strong) UIImageView *backImg;
@property(nonatomic, strong) UIImageView *topJianBian;
@property(nonatomic, strong) UIImageView *camearImg;
@property(nonatomic, strong) UIImageView *img;
@property(nonatomic, strong) UILabel *photoCountLabel;
@property(nonatomic, strong) NSDictionary *dataDic;
@property(nonatomic, strong) UITableView *footerTableView;
@property(nonatomic, strong) NSMutableArray *footerDataArr;

@property(nonatomic, strong) CoreDataManager *manager;
@property(nonatomic, strong) UIButton *likeBtn;
@property(nonatomic, assign) BOOL isLake; // 判断是否收藏
@property(nonatomic, strong) NSMutableArray *coreDataArr;

@property(nonatomic, strong) UIButton *shareBtn;
@end

@implementation MZRecommendLastViewController
- (void)dealloc
{
    [UMSocialShakeService unShakeToSns];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    
    self.footerDataArr = [NSMutableArray array];
    self.coreDataArr = [NSMutableArray array];
    
    [self prepareData];
    [self createTableView];
    
    self.naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.naviView.backgroundColor = [UIColor colorWithRed:62 / 255. green:176 / 255. blue:192 / 255. alpha:1];
    self.naviView.alpha = 0;
    [self.view addSubview:self.naviView];
    
    self.naviLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 100, 25, 200, 25)];
    self.naviLabel.textColor = [UIColor whiteColor];
    self.naviLabel.textAlignment = NSTextAlignmentCenter;
    self.naviLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:19];
    self.naviLabel.text = self.tempModel.name;
    [self.naviView addSubview:self.naviLabel];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, 25, 25, 25);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-iconfonticonfontjiantouzuo11@2x"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUB show:YES];
    self.HUB.labelText = @"正在加载";
    
    //收藏按钮
    self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.likeBtn.frame = CGRectMake(WIDTH - 100, 20, 25, 25);
    [self.likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.likeBtn];
    self.likeBtn.hidden = YES;
    
    //分享按钮
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareBtn.frame = CGRectMake(WIDTH - 50, 15, 30, 30);
    [self.shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [self.shareBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    [self.view addSubview:self.shareBtn];
    self.shareBtn.hidden = YES;
    
    self.manager = [CoreDataManager shareInstance];
    //    NSLog(@"%@", [self.manager applicationDocumentsDirectory]);
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MuDiDi" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    if (fetchedObjects.count == 0) {
        
        [self.likeBtn setBackgroundImage:[UIImage imageNamed:@"whiteHeart"] forState:UIControlStateNormal];
    }else{
        
        self.coreDataArr = [NSMutableArray arrayWithArray:fetchedObjects];
        for (MuDiDi *model in self.coreDataArr) {
            
            if ([model.name isEqualToString:self.tempModel.name]) {
                
                self.isLake = YES;
                break;
            }
        }
        
        if (self.isLake == NO) {
            
            [self.likeBtn setBackgroundImage:[UIImage imageNamed:@"whiteHeart"] forState:UIControlStateNormal];
            
        }else{
            
            [self.likeBtn setBackgroundImage:[UIImage imageNamed:@"redHeart"] forState:UIControlStateNormal];
            
        }
    }
    
    //摇一摇分享
    [UMSocialShakeService setShakeToShareWithTypes:@[UMShareToSina,UMShareToTencent,UMShareToQzone,UMShareToRenren,UMShareToDouban]
                                         shareText:[NSString stringWithFormat:@"「%@」\nhttp://web.breadtrip.com/scenic/%@/%@/",self.tempModel.name, self.tempModel.type, self.tempModel.valueId]
                                      screenShoter:[UMSocialScreenShoterDefault screenShoter]
                                  inViewController:self
                                          delegate:nil];
    [UMSocialShakeService setShakeThreshold:1.5];
}


#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGTH + 40) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(HEIGTH / 4 + 20 , 0, 0, 0);
    [self.tableView registerClass:[MZRecommendLastTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.backgroundColor = self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    [self.view addSubview:self.tableView];
    
    self.backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, -HEIGTH / 4 - 40, WIDTH, HEIGTH / 4 + 40)];
    self.backImg.contentMode = UIViewContentModeScaleAspectFill; //设置img高度改变时 宽度也改变
    self.backImg.clipsToBounds = YES;
    self.backImg.userInteractionEnabled = YES;
    [self.tableView addSubview:self.backImg];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.backImg addGestureRecognizer:tap];
    
    self.topJianBian = [[UIImageView alloc] initWithFrame:CGRectMake(0, -HEIGTH / 4 - 40, WIDTH, HEIGTH / 4 + 40)];
    self.topJianBian.image = [UIImage imageNamed:@"Topjiabian"];
    [self.tableView addSubview:self.topJianBian];
    
    //渐变色图片
    self.img = [[UIImageView alloc]initWithFrame:CGRectMake(0, -HEIGTH / 4 - 40, WIDTH, -HEIGTH / 4 - 40)];
    self.img.userInteractionEnabled = NO;
    self.img.image = [UIImage imageNamed:@"jiabian"];
    [self.tableView addSubview:self.img];
    
    self.photoCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 70, -25, 60, 20)];
    self.photoCountLabel.textColor = [UIColor colorWithRed:1 green:0.98 blue:1 alpha:1];
    self.photoCountLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    self.photoCountLabel.shadowColor = [UIColor colorWithRed:0.75 green:0.82 blue:0.76 alpha:1];
    self.photoCountLabel.textAlignment = NSTextAlignmentRight;
    [self.tableView addSubview:self.photoCountLabel];
    
    self.camearImg = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 70, -27, 22, 22)];
    [self.tableView addSubview:self.camearImg];
    
}

#pragma mark - 准备tableView数据
- (void)prepareData
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@", self.tempModel.type, self.tempModel.valueId];
    [MZNetHandle getUrl:str cookie:nil result:^(id result) {
       
        self.dataDic = [NSDictionary dictionaryWithDictionary:result];
        
        //控件赋值
        self.photoCountLabel.text = [NSString stringWithFormat:@"%@", self.dataDic[@"photo_count"]];
        self.camearImg.image = [UIImage imageNamed:@"iconfont-xiangji@2x"];
        [self.backImg sd_setImageWithURL:[NSURL URLWithString:self.tempModel.cover] placeholderImage:[UIImage imageNamed:@"TheMostBig"]];
        self.likeBtn.hidden = NO;
        self.shareBtn.hidden = NO;
        
        //尾部tableView数据
        NSMutableArray *mArr = [NSMutableArray array];
        if ([result[@"description"] length] != 0) {
            
            NSDictionary *dic = @{@"概况" : self.dataDic[@"description"]};
            [mArr addObject:dic];
        }
        if ([result[@"address"] length] != 0){
            
            NSDictionary *dic = @{@"地址" : self.dataDic[@"address"]};
            [mArr addObject:dic];
        }
        if ([result[@"arrival_type"] length] != 0){
            
            NSDictionary *dic = @{@"到达方法" : self.dataDic[@"arrival_type"]};
            [mArr addObject:dic];
        }
        if ([result[@"opening_time"] length] != 0){
            
            NSDictionary *dic = @{@"开放时间" : self.dataDic[@"opening_time"]};
            [mArr addObject:dic];
        }
        if ([result[@"fee"] length] != 0){
            
            NSDictionary *dic = @{@"门票价格" : self.dataDic[@"fee"]};
            [mArr addObject:dic];
        }
        if ([result[@"tel"] length] != 0){
            
            NSDictionary *dic = @{@"联系方式" : self.dataDic[@"tel"]};
            [mArr addObject:dic];
        }
        if ([result[@"website"] length] != 0){
            
            NSDictionary *dic = @{@"官方网站" : self.dataDic[@"website"]};
            [mArr addObject:dic];
        }
        
        [self.footerDataArr addObjectsFromArray:mArr];
        
        [self.footerTableView reloadData];
        [self.tableView reloadData];
        [self.HUB removeFromSuperview];
    }];
}

#pragma mark - tableView协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        
        MZRecommendLastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
        cell.tempDic = self.dataDic[@"impressions"][@"trips"][indexPath.row];
        return cell;
        
    }else{
        
        MZFooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"footerCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
        cell.tempDic = self.footerDataArr[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSArray *arr = [self.footerDataArr[indexPath.row] allKeys];
        if ([arr[0] isEqualToString:@"概况"] || [arr[0] isEqualToString:@"地址"] || [arr[0] isEqualToString:@"联系方式"] || [arr[0] isEqualToString:@"官方网站"]) {
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        return cell;
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        
        return [self.dataDic[@"impressions"][@"trips"] count];
    }else
    {
        return self.footerDataArr.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        
        CGFloat hSubtitle = [[self class]heightWithText:self.dataDic[@"impressions"][@"trips"][indexPath.row][@"trip_text"] fontSize:14 Width:WIDTH - 20];
        
        //判断副标题是否有内容
        if ([self.dataDic[@"impressions"][@"trips"][indexPath.row][@"trip_text"] length] != 0) {
            
            return  10 +  WIDTH / 9 + 15 + hSubtitle +  10 +  150 + 20 ;
        }
        else
        {
            return  10 +  WIDTH / 9  +  10 +  150 + 20 ;
        }
    }else
    {
        
        return 80;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == self.tableView) {
        
        YBTraveldetialViewController *tdVC = [[YBTraveldetialViewController alloc] init];
        tdVC.valueId = (NSString *)self.dataDic[@"id"];
        tdVC.whoPush = @"mz";
        [self presentViewController:tdVC animated:YES completion:^{
        }];
        
    }else{
        NSArray *arr = [self.footerDataArr[indexPath.row] allKeys];
        
        if ([arr[0] isEqualToString:@"概况"]) {
            
            MZDescriptionViewController *desVC = [[MZDescriptionViewController alloc] init];
            desVC.tempStr = self.footerDataArr[indexPath.row][arr[0]];
            desVC.naviTitle = self.dataDic[@"name"];
            [self.navigationController pushViewController:desVC animated:YES];
        }
        if ([arr[0] isEqualToString:@"地址"]) {
            
            MZAddressViewController *addressVC = [[MZAddressViewController alloc] init];
            addressVC.naviTitle = self.dataDic[@"name"];
            addressVC.locationDic = self.dataDic[@"location"];
            [self.navigationController pushViewController:addressVC animated:YES];
        }
        if ([arr[0] isEqualToString:@"联系方式"]) {
            
            [self makePhone:self.footerDataArr[indexPath.row][arr[0]]];
            
        }
        if ([arr[0] isEqualToString:@"官方网站"]) {
            
            MZWebsiteViewController *webVC = [[MZWebsiteViewController alloc] init];
            webVC.webStr = self.footerDataArr[indexPath.row][arr[0]];
            webVC.naviTitle = self.dataDic[@"name"];
            [self.navigationController pushViewController:webVC animated:YES];
        }
    }
}

#pragma mark - tableView 头部分区高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return 240;
    }else{
        return 0;
    }
}
#pragma mark - 分区头view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 240)];
        view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
        
        UIView *iconView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 2 - 35, -35, 70, 70)];
        iconView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.93 alpha:1];
        iconView.layer.cornerRadius = 35;
        [view addSubview:iconView];
        
        UIImageView *imgIcon = [[UIImageView alloc]init];
        imgIcon.frame = CGRectMake(WIDTH / 2 - 32, -32, 64, 64);
        imgIcon.layer.masksToBounds = YES;
        imgIcon.layer.cornerRadius = 32;
        imgIcon.backgroundColor = [UIColor orangeColor];
        [view addSubview:imgIcon];
        
        //标题
        UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, WIDTH - 40, 30)];
        labelTitle.textAlignment = NSTextAlignmentCenter;
        labelTitle.text = self.tempModel.name;
        labelTitle.font = [UIFont systemFontOfSize:24];
        [view addSubview:labelTitle];
        
        //评分
        StarView *star = [[StarView alloc]initWithFrame:CGRectMake((WIDTH - 83)/2 , labelTitle.frame.origin.y + labelTitle.frame.size.height + 10 , 15 * 5 + 8, 15) rating:self.tempModel.rating.integerValue];
        [view addSubview:star];
        
        //描述
        UILabel *aboutLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, star.frame.origin.y + 20, WIDTH - 100, 75)];
        aboutLabel.textAlignment = NSTextAlignmentCenter;
        aboutLabel.numberOfLines = 0;
        aboutLabel.font = [UIFont systemFontOfSize:13];
        aboutLabel.text = self.tempModel.recommended_reason;
        [view addSubview:aboutLabel];
        
        /* 横线 点 标题*/
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(20 ,labelTitle.frame.origin.y + labelTitle.frame.size.height + 45 + 30 + 50, (WIDTH - 100 - 30 - 30 - 40) / 2, 1);
        label.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
        [view addSubview:label];
        
        UILabel *labelPoint = [[UILabel alloc]init];
        labelPoint.frame = CGRectMake(label.frame.origin.x + label.frame.size.width + 10, label.frame.origin.y - 2, 5, 5);
        labelPoint.layer.masksToBounds = YES;
        labelPoint.layer.cornerRadius = 2.5;
        labelPoint.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
        [view addSubview:labelPoint];
        
        UILabel *labelImpression = [[UILabel alloc]init];
        labelImpression.frame = CGRectMake(labelPoint.frame.origin.x + 15, labelPoint.frame.origin.y - 10, 100, 30);
        labelImpression.textAlignment = NSTextAlignmentCenter;
        labelImpression.text = @"走走印象";
        labelImpression.textColor = [UIColor darkTextColor];
        labelImpression.center = CGPointMake(labelTitle.center.x, labelPoint.frame.origin.y - 3 );
        labelImpression.font = [UIFont systemFontOfSize:17];
        labelImpression.textColor = [UIColor darkTextColor];
        [view addSubview:labelImpression];
        
        UILabel *labelPoint1 = [[UILabel alloc]init];
        labelPoint1.frame = CGRectMake(labelImpression.frame.origin.x + labelImpression.frame.size.width + 10, labelPoint.frame.origin.y , 5, 5);
        labelPoint1.layer.masksToBounds = YES;
        labelPoint1.layer.cornerRadius = 2.5;
        labelPoint1.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
        [view addSubview:labelPoint1];
        
        UILabel *label1 = [[UILabel alloc]init];
        label1.frame = CGRectMake(labelPoint1.frame.origin.x + 2 + 15, labelPoint1.frame.origin.y + 2, label.frame.size.width, 1);
        label1.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
        [view addSubview:label1];
        
        return  view;

    }else
    {
        return nil;
    }
}

#pragma mark - tableView 尾部视图高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        
        return 150 + self.footerDataArr.count * 80;
        
    }else
    {
        return 0;
    }
}

#pragma mark - 分区尾tableView
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        if (self.dataDic.count != 0) {
            
            UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
            footerView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
            
            UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            moreBtn.frame = CGRectMake(WIDTH / 2 - 100, 20, 200, 50);
            moreBtn.layer.borderWidth = 1;
            moreBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
            moreBtn.layer.cornerRadius = 30;
            moreBtn.titleLabel.font = [UIFont systemFontOfSize:19];
            [moreBtn setTitle:@"更多印象" forState:UIControlStateNormal];
            [moreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [moreBtn addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
            [footerView addSubview:moreBtn];
            
            /* 横线 点 标题*/
            UILabel *label = [[UILabel alloc]init];
            label.frame = CGRectMake(20 ,100, (WIDTH - 100 - 30 - 30 - 40) / 2, 1);
            label.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
            [footerView addSubview:label];
            
            UILabel *labelPoint = [[UILabel alloc]init];
            labelPoint.frame = CGRectMake(label.frame.origin.x + label.frame.size.width + 10, label.frame.origin.y - 2, 5, 5);
            labelPoint.layer.masksToBounds = YES;
            labelPoint.layer.cornerRadius = 2.5;
            labelPoint.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
            [footerView addSubview:labelPoint];
            
            UILabel *labelImpression = [[UILabel alloc]init];
            labelImpression.frame = CGRectMake(labelPoint.frame.origin.x + 15, labelPoint.frame.origin.y - 13, 100, 30);
            labelImpression.textAlignment = NSTextAlignmentCenter;
            labelImpression.text = @"基本信息";
            labelImpression.textColor = [UIColor darkTextColor];
            labelImpression.font = [UIFont systemFontOfSize:17];
            labelImpression.textColor = [UIColor darkTextColor];
            [footerView addSubview:labelImpression];
            
            UILabel *labelPoint1 = [[UILabel alloc]init];
            labelPoint1.frame = CGRectMake(labelImpression.frame.origin.x + labelImpression.frame.size.width + 10, labelPoint.frame.origin.y , 5, 5);
            labelPoint1.layer.masksToBounds = YES;
            labelPoint1.layer.cornerRadius = 2.5;
            labelPoint1.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
            [footerView addSubview:labelPoint1];
            
            UILabel *label1 = [[UILabel alloc]init];
            label1.frame = CGRectMake(labelPoint1.frame.origin.x + 2 + 15, labelPoint1.frame.origin.y + 2, label.frame.size.width, 1);
            label1.backgroundColor = [UIColor colorWithRed:0.74 green:0.72 blue:0.6 alpha:1];
            [footerView addSubview:label1];
            
            //创建tableView的尾部视图
            self.footerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 150, WIDTH, self.footerDataArr.count * 80) style:UITableViewStylePlain];
            self.footerTableView.delegate = self;
            self.footerTableView.dataSource = self;
            self.footerTableView.scrollEnabled = NO;
            [self.footerTableView registerClass:[MZFooterTableViewCell class] forCellReuseIdentifier:@"footerCell"];
            [footerView addSubview:self.footerTableView];
        return footerView;
    }
        else {
            return nil;
        }
        
    }else{
        
        return nil;
    }
    
}

#pragma mark - 背景图下滑改变宽高
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取当前偏移量
    CGFloat y = scrollView.contentOffset.y;
    if (y < - HEIGTH / 4 + 40) {
        CGRect frame = self.backImg.frame;
        frame.origin.y = y; //image 的frame不断往上偏移
        frame.size.height = -y;//collectionview向下偏移了多少 高度就增加多少
        self.backImg.frame = frame;
        self.img.frame = frame; //渐变色图片
        self.topJianBian.frame = frame;
    }
    //bar 将要出来时  显隐度一点点改变
    if (scrollView.contentOffset.y <= 0 && scrollView.contentOffset.y >= -64) {
        self.naviView.alpha = (64 + scrollView.contentOffset.y) / 64;
    }
    else if (scrollView.contentOffset.y > 0)
    {
        self.naviView.alpha = 1.0;
    }
    else
    {
        self.naviView.alpha = 0.0;
    }
}

#pragma mark - 下滑跳转页面
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView.contentOffset.y < - HEIGTH / 3 - 60) {
        
        MZRecommendPhotosViewController *photosVC = [[MZRecommendPhotosViewController alloc] init];
        photosVC.tempModel = self.tempModel;
        [self presentViewController:photosVC animated:YES completion:^{
        }];
        
    }
}

#pragma mark - 轻拍方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    MZRecommendPhotosViewController *photosVC = [[MZRecommendPhotosViewController alloc] init];
    photosVC.tempModel = self.tempModel;
    [self presentViewController:photosVC animated:YES completion:^{
    }];
}

#pragma mark - 打电话方法
- (void)makePhone: (NSString *)phoneNumber;
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"是否进行电话咨询\n%@", phoneNumber] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //调用打电话方法
        NSString *telStr = [NSString stringWithFormat:@"tel://%@", phoneNumber];
        NSURL *url = [NSURL URLWithString:telStr];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        else
        {
            UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"号码不可用\n或您的备设不支持拨号功能" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert1 addAction:okAction1];
            [self presentViewController:alert1 animated:YES completion:nil];
        }
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)moreAction
{
    
}

#pragma mark - 收藏方法 登录
- (void)likeAction:(UIButton *)sender
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"judge"] == NO) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录,才能收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
            landingViewController *login = [[landingViewController alloc] init];
            [self.navigationController pushViewController:login animated:YES];
            
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"judge"];
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    } else {
        
        if (self.isLake == NO) {
            
            self.isLake = YES;
            MuDiDi *mudidi = [NSEntityDescription insertNewObjectForEntityForName:@"MuDiDi" inManagedObjectContext:self.manager.managedObjectContext];
            mudidi.name = self.tempModel.name;
            mudidi.cover = self.tempModel.cover;
            mudidi.rating = self.tempModel.rating;
            mudidi.type = self.tempModel.type;
            mudidi.valueId = self.tempModel.valueId;
            mudidi.visited_count = self.tempModel.visited_count;
            [self.manager saveContext];
            [self.likeBtn setBackgroundImage:[UIImage imageNamed:@"redHeart"] forState:UIControlStateNormal];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self performSelector:@selector(delayDismiss) withObject:alert afterDelay:1];
            
        }else
        {
            self.isLake = NO;
            for (MuDiDi *mudidi in self.coreDataArr) {
                
                if ([mudidi.name isEqualToString:self.tempModel.name]) {
                    
                    [self.manager.managedObjectContext deleteObject:mudidi];
                }
            }
            [self.manager saveContext];
            [self.likeBtn setBackgroundImage:[UIImage imageNamed:@"whiteHeart"] forState:UIControlStateNormal];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"取消收藏" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:nil];
            [self performSelector:@selector(delayDismiss) withObject:alert afterDelay:1];
        }
    }
}

#pragma mark -分享
- (void)shareAction
{
    UIImageView *img = [[UIImageView alloc] init];
    img.frame = CGRectMake(100, 100, 100, 100);
    [img sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"share_args"][@"default"][@"shr_image"]] placeholderImage:[UIImage imageNamed:@""]];
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5661bb71e0f55a4f60003bca"
                                      shareText:[NSString stringWithFormat:@"「%@」\n%@",self.dataDic[@"share_args"][@"default"][@"shr_text"], self.dataDic[@"share_args"][@"default"][@"shr_url"]]
                                     shareImage:img.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,UMShareToTencent,UMShareToWechatTimeline,UMShareToQzone,UMShareToQQ,UMShareToRenren,UMShareToDouban,UMShareToEmail,UMShareToSms,UMShareToFacebook,UMShareToTwitter,nil]
                                       delegate:nil];
}

- (void)delayDismiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
